<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use \think\Request;
use think\facade\View;
use app\common\model\GoodsList;
use app\common\model\Shang;
use app\common\model\RankWeek;
use app\common\model\RankMonth;
use app\common\model\User;
use think\facade\Db;

class Rank extends Base
{

    /**
     * 周榜奖品
     */
    public function week()
    {
        $title = trim(input('get.title'));
        $whe = array();
        $whe[] = ['goods_id', '=', -1];
        if (!empty($title)) {
            $whe[] = ['title', 'like', ' % ' . $title . ' % '];
        }
        $field = "*";
        $order = "rank asc,id asc";
        $data = GoodsList::getList($whe, $field, $order, $this->page);
        foreach ($data['list'] as &$value) {
            $value['shang'] = Shang::where(['id' => $value['shang_id']])->value('title');
        }
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        return View::fetch("Rank/week");
    }

    /**
     * 周榜奖品添加
     */
    public function week_add(Request $request)
    {
        if (!$request->isPost()) {
            $shang = Shang::where('id', 'between', [10, 33])->select()->toArray();
            View::assign('shang', $shang);
            return View::fetch('Rank/week_add');
        } else {
            $data = input('post.');
            if (empty($data['rank'])) {
                return $this->renderError('请输入奖品排名');
            }
            if (RegInt($data['rank'])) {
                return $this->renderError('奖品排名请输入大于0的整数');
            }
            $rank_is = GoodsList::field('id')->where('goods_id', -1)->where('rank', $data['rank'])->find();
            if ($rank_is) {
                return $this->renderError('奖品排名已存在');
            }
            if (empty($data['title'])) {
                return $this->renderError('请输入奖品名称');
            }
            if (empty($data['shang_id'])) {
                return $this->renderError('请选择奖品类型');
            }
            if (RegMoney($data['price'])) {
                return $this->renderError('奖品售价设置错误,最多保留两位小数');
            }
            if (RegMoney($data['money'])) {
                return $this->renderError('兑换价格设置错误,最多保留两位小数');
            }
            if (empty($data['imgurl'])) {
                return $this->renderError('请上传图片');
            }
            $prize_code = getPrizeCode() . '_' . time();
            $data['prize_code'] = $prize_code;
            $data['goods_id'] = -1;
            $data['addtime'] = time();
            $dd = (new GoodsList())->insert($data);
            if ($dd) {
                return $this->renderSuccess('添加成功');
            } else {
                return $this->renderError('添加失败');
            }
        }
    }

    /**
     * 周榜奖品编辑
     */
    public function week_edit(Request $request)
    {
        if (!$request->isPost()) {
            $id = request()->param('id/d', 0);
            $info = GoodsList::where(['id' => $id])->find();
            if (!$info) {
                return $this->renderError('请求参数错误');
            }
            $shang = Shang::where('id', 'between', [10, 33])->select()->toArray();
            View::assign('shang', $shang);
            View::assign('info', $info);
            return View::fetch('Rank/week_edit');
        } else {
            $data = input('post.');
            if (empty($data['id'])) {
                return $this->renderError('请求参数错误');
            }
            $info = GoodsList::where(['id' => $data['id']])->find();
            if (!$info) {
                return $this->renderError('请求参数错误1');
            }
            if (empty($data['rank'])) {
                return $this->renderError('请输入奖品排名');
            }
            if (RegInt($data['rank'])) {
                return $this->renderError('奖品排名请输入大于0的整数');
            }
            $rank_is = GoodsList::field('id')
                ->where('id', '<>', $info['id'])
                ->where('goods_id', -1)
                ->where('rank', $data['rank'])
                ->find();
            if ($rank_is) {
                return $this->renderError('奖品排名已存在');
            }
            if (empty($data['title'])) {
                return $this->renderError('请输入奖品名称');
            }
            if (empty($data['shang_id'])) {
                return $this->renderError('请选择奖品类型');
            }
            if (RegMoney($data['price'])) {
                return $this->renderError('奖品售价设置错误,最多保留两位小数');
            }
            if (RegMoney($data['money'])) {
                return $this->renderError('兑换价格设置错误,最多保留两位小数');
            }
            if (empty($data['imgurl'])) {
                return $this->renderError('请上传图片');
            }
            $data['update_time'] = time();
            unset($data['id']);
            $dd = $info->allowField([])->save($data);
            if ($dd) {
                return $this->renderSuccess('编辑成功');
            } else {
                return $this->renderError('编辑失败');
            }
        }
    }

    /**
     * 周榜奖品删除
     */
    public function week_del(Request $request)
    {
        $id = \request()->param('id/d', 0);
        $info = GoodsList::field('prize_code')
            ->where(['id' => $id])
            ->find();
        if (!$info) {
            return $this->renderError("请勿重复操作");
        }
        #删除奖品
        $res = GoodsList::field('id')
            ->where(['id' => $id])
            ->delete();
        if ($res) {
            return $this->renderSuccess("操作成功");
        } else {
            return $this->renderError("操作失败");
        }
    }

    /**
     * 周榜记录
     */
    public function week_log()
    {
        $user_id = trim(input('get.user_id'));
        $mobile = trim(input('get.mobile'));
        $whe = array();
        if ($user_id) {
            $whe[] = ['user_id', '=', $user_id];
        }
        if ($mobile) {
            $mobile = User::field('id')->where('mobile', '=', $mobile)->value('id');
            $whe[] = ['user_id', '=', $mobile];
        }
        $field = "*";
        $order = "addtime desc,id asc";
        $data = RankWeek::getList($whe, $field, $order, 30);
        foreach ($data['list'] as &$value) {
            $user_info = User::field('nickname,headimg,mobile')->where(['id' => $value['user_id']])->find();
            $value['nickname'] = $user_info['nickname'];
            $value['headimg'] = $user_info['headimg'];
            $value['mobile'] = $user_info['mobile'];
        }
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        return View::fetch("Rank/week_log");
    }


    /**
     * 月榜奖品
     */
    public function month()
    {
        $title = trim(input('get.title'));
        $whe = array();
        $whe[] = ['goods_id', '=', -2];
        if (!empty($title)) {
            $whe[] = ['title', 'like', ' % ' . $title . ' % '];
        }
        $field = "*";
        $order = "rank asc,id asc";
        $data = GoodsList::getList($whe, $field, $order, $this->page);
        foreach ($data['list'] as &$value) {
            $value['shang'] = Shang::where(['id' => $value['shang_id']])->value('title');
        }
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        return View::fetch("Rank/month");
    }

    /**
     * 月榜奖品添加
     */
    public function month_add(Request $request)
    {
        if (!$request->isPost()) {
            $shang = Shang::where('id', 'between', [10, 33])->select()->toArray();
            View::assign('shang', $shang);
            return View::fetch('Rank/month_add');
        } else {
            $data = input('post.');
            if (empty($data['rank'])) {
                return $this->renderError('请输入奖品排名');
            }
            if (RegInt($data['rank'])) {
                return $this->renderError('奖品排名请输入大于0的整数');
            }
            $rank_is = GoodsList::field('id')
                ->where('goods_id', -2)
                ->where('rank', $data['rank'])->find();
            if ($rank_is) {
                return $this->renderError('奖品排名已存在');
            }
            if (empty($data['title'])) {
                return $this->renderError('请输入奖品名称');
            }
            if (empty($data['shang_id'])) {
                return $this->renderError('请选择奖品类型');
            }
            if (RegMoney($data['price'])) {
                return $this->renderError('奖品售价设置错误,最多保留两位小数');
            }
            if (RegMoney($data['money'])) {
                return $this->renderError('兑换价格设置错误,最多保留两位小数');
            }
            if (empty($data['imgurl'])) {
                return $this->renderError('请上传图片');
            }
            $prize_code = getPrizeCode() . '_' . time();
            $data['prize_code'] = $prize_code;
            $data['goods_id'] = -2;
            $data['addtime'] = time();
            $dd = (new GoodsList())->insert($data);
            if ($dd) {
                return $this->renderSuccess('添加成功');
            } else {
                return $this->renderError('添加失败');
            }
        }
    }

    /**
     * 月榜奖品编辑
     */
    public function month_edit(Request $request)
    {
        if (!$request->isPost()) {
            $id = request()->param('id/d', 0);
            $info = GoodsList::where(['id' => $id])->find();
            if (!$info) {
                return $this->renderError('请求参数错误');
            }
            $shang = Shang::where('id', 'between', [10, 33])->select()->toArray();
            View::assign('shang', $shang);
            View::assign('info', $info);
            return View::fetch('Rank/month_edit');
        } else {
            $data = input('post.');
            if (empty($data['id'])) {
                return $this->renderError('请求参数错误');
            }
            $info = GoodsList::where(['id' => $data['id']])->find();
            if (!$info) {
                return $this->renderError('请求参数错误1');
            }
            if (empty($data['rank'])) {
                return $this->renderError('请输入奖品排名');
            }
            if (RegInt($data['rank'])) {
                return $this->renderError('奖品排名请输入大于0的整数');
            }
            $rank_is = GoodsList::field('id')
                ->where('id', '<>', $info['id'])
                ->where('goods_id', -2)
                ->where('rank', $data['rank'])
                ->find();
            if ($rank_is) {
                return $this->renderError('奖品排名已存在');
            }
            if (empty($data['title'])) {
                return $this->renderError('请输入奖品名称');
            }
            if (empty($data['shang_id'])) {
                return $this->renderError('请选择奖品类型');
            }
            if (RegMoney($data['price'])) {
                return $this->renderError('奖品售价设置错误,最多保留两位小数');
            }
            if (RegMoney($data['money'])) {
                return $this->renderError('兑换价格设置错误,最多保留两位小数');
            }
            if (empty($data['imgurl'])) {
                return $this->renderError('请上传图片');
            }
            $data['update_time'] = time();
            unset($data['id']);
            $dd = $info->allowField([])->save($data);
            if ($dd) {
                return $this->renderSuccess('编辑成功');
            } else {
                return $this->renderError('编辑失败');
            }
        }
    }

    /**
     * 月榜奖品删除
     */
    public function month_del(Request $request)
    {
        $id = \request()->param('id/d', 0);
        $info = GoodsList::field('prize_code')
            ->where(['id' => $id])
            ->find();
        if (!$info) {
            return $this->renderError("请勿重复操作");
        }
        #删除奖品
        $res = GoodsList::field('id')
            ->where(['id' => $id])
            ->delete();
        if ($res) {
            return $this->renderSuccess("操作成功");
        } else {
            return $this->renderError("操作失败");
        }
    }

    /**
     * 月榜记录
     */
    public function month_log()
    {
        $user_id = trim(input('get.user_id'));
        $mobile = trim(input('get.mobile'));
        $whe = array();
        if ($user_id) {
            $whe[] = ['user_id', '=', $user_id];
        }
        if ($mobile) {
            $mobile = User::field('id')->where('mobile', '=', $mobile)->value('id');
            $whe[] = ['user_id', '=', $mobile];
        }
        $field = "*";
        $order = "addtime desc,id asc";
        $data = RankMonth::getList($whe, $field, $order, 30);
        foreach ($data['list'] as &$value) {
            $user_info = User::field('nickname,headimg,mobile')->where(['id' => $value['user_id']])->find();
            $value['nickname'] = $user_info['nickname'];
            $value['headimg'] = $user_info['headimg'];
            $value['mobile'] = $user_info['mobile'];
        }
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        return View::fetch("Rank/month_log");
    }


}
